#!/bin/bash
# this file is used to pull images and push images

# check args make usre pull or push
if [ $# -lt 1 ]; then
    echo "Usage: $0 <pull|push> <all>"
    exit 1
fi

ALL=[  $2 -eq 'all' ]

# check if docker is installed
if ! command -v docker &> /dev/null; then
    echo "docker is not installed"
    exit 1
fi

# check if docker is running
if ! docker info &> /dev/null; then
    echo "docker is not running"
    exit 1
fi

ALIYUN_SERVER=registry.cn-beijing.aliyuncs.com
CANGLING_SERVER=hub.cangling.cn
docker login --username=zhangjianshe@gmail.com -pImagebot__ ${ALIYUN_SERVER}


# pull images 
# such as pull_images "mapway/zookeeper:3.9" "bitnami/zookeeper:3.9"  
function pull_images() {
    TARGET_NAME=$1
    IMAGE_NAME=$2
 
    # check if image is exist
    if ! docker pull $ALIYUN_SERVER/$TARGET_NAME; then
        echo "image $ALIYUN_SERVER/$TARGET_NAME is not exist"
        exit 1
    fi
    docker tag $ALIYUN_SERVER/$TARGET_NAME $CANGLING_SERVER/$IMAGE_NAME
}

# push images
# such as pull_images "bitnami/zookeeper:3.9" "mapway/zookeeper:3.9" 
function push_images() {
    IMAGE_NAME=$1
    TARGET_NAME=$2
    docker tag  $CANGLING_SERVER/$IMAGE_NAME $ALIYUN_SERVER/$TARGET_NAME
    docker push $ALIYUN_SERVER/$TARGET_NAME
}


CMD=$1

case $CMD in
    pull)
        pull_images "mapway/zookeeper:3.9" "bitnami/zookeeper:3.9" 
        pull_images "mapway/kafka:3.9" "bitnami/kafka:3.9" 
        pull_images "mapway/influxdb:2" "mapway/influxdb:2" 
        pull_images "mapway/postgis:13.0" "mapway/postgis:13.0" 
        pull_images "mapway/postgis:17" "mapway/postgis:17" 
        pull_images "mapway/emqx:v1.0" "mapway/emqx:v1.0" 
        pull_images "mapway/redis:v1" "mapway/redis:v1" 
        pull_images "mapway/mongo:5.0.3" "mapway/mongo:5.0.3" 
        pull_images "mapway/ai-geoserver:1.0" "mapway/ai-geoserver:1.0" 
        pull_images "mapway/nginx:1.14.2" "mapway/nginx:1.14.2" 
        pull_images "mapway/traefik:3.3" "mapway/traefik:3.3" 
        pull_images "mapway/casdoor:v1.854.0" "casbin/casdoor:v1.854.0" 
        pull_images "mapway/gitlab-ce:17.9.1-ce.0" "gitlab/gitlab-ce:17.9.1-ce.0" 
        
        if [ $ALL ]; then
            pull_images "mapway/iot-server:1.0" "mapway/iot-server:1.0" 
            pull_images "mapway/cis-map:1.0" "mapway/cis-map:1.0" 
            pull_images "mapway/cis-server:1.0" "mapway/cis-server:1.0" 
            pull_images "mapway/cis-k8s:1.0" "mapway/cis-k8s:1.0" 
            pull_images "mapway/cis-im:1.0" "mapway/cis-im:1.0" 
        fi
        ;;
    push)
        push_images "bitnami/zookeeper:3.9" "mapway/zookeeper:3.9" 
        push_images "bitnami/kafka:3.9" "mapway/kafka:3.9" 
        push_images "mapway/influxdb:2" "mapway/influxdb:2" 
        push_images "mapway/postgis:13.0" "mapway/postgis:13.0" 
        push_images "mapway/postgis:17" "mapway/postgis:17" 
        push_images "mapway/emqx:v1.0" "mapway/emqx:v1.0" 
        push_images "mapway/redis:v1" "mapway/redis:v1" 
        push_images "mapway/mongo:5.0.3" "mapway/mongo:5.0.3" 
        push_images "mapway/ai-geoserver:1.0" "mapway/ai-geoserver:1.0" 
        push_images "mapway/nginx:1.14.2" "mapway/nginx:1.14.2" 
        push_images "mapway/traefik:3.3" "mapway/traefik:3.3" 
        push_images "casbin/casdoor:v1.854.0" "mapway/casdoor:v1.854.0" 
        push_images "gitlab/gitlab-ce:17.9.1-ce.0" "mapway/gitlab-ce:17.9.1-ce.0" 

        if [ $ALL ]; then
            push_images "mapway/iot-server:1.0" "mapway/iot-server:1.0" 
            push_images "mapway/cis-map:1.0" "mapway/cis-map:1.0" 
            push_images "mapway/cis-server:1.0" "mapway/cis-server:1.0" 
            push_images "mapway/cis-k8s:1.0" "mapway/cis-k8s:1.0" 
            push_images "mapway/cis-im:1.0" "mapway/cis-im:1.0" 
        fi

        ;;
    *)
        echo "Usage: $0 <pull|push>"
        exit 1
        ;;
esac
